// pages/driver/wallet/wallet.js
const handler = require("../../../utils/function.js");
var config = require("../../../config.js");
const app = getApp();
var api = require("../../../api.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgs: handler.userImg,
    img: handler.driverImg,
    TodayIncome: 0, //今日收入
    Spending: 0, //总支出
    Income: 0, //总收入
    Refund: 0, //退款
    balance: 0, //余额
    actions: config.extract,        //调用配置文件支付设置
    cancelText: '取消',
    page: 1,
    list: [],
    pageSize: 20,
    start_time: '',
    hasMoreData: true,
    distribution: 1,
  },
  intoPageHandler: function (e) {

    handler.intoPageHandler(e.currentTarget.dataset.skip);
  },
  // openActionsheet 立即提现
  openActionsheet() {
    this.setData({
      'show': true
    });
  },
  //获取时间
  bindTimeChange: function (e) {
    let that = this;
    that.setData({ page: 1, start_time: e.detail.value })
    that.get_info_list();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('提现', options)
    // 获取优惠券
    if (options.uid) {
      app.shareCouponsMessage(options.uid)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.get_info_list()
  },
  // 获取数据
  get_info_list() {
    let that = this;
    app.request({
      url: api.wallet_log.index,
      method: 'post',
      data: {
        page: that.data.page,
        start_time: that.data.start_time,
      },
      success: function (res) {

        console.log(res)
        console.log(res.code)
        var lists = that.data.list;
        if (res.code == 1) {
          that.setData({
            TodayIncome: res.data.user.TodayIncome, //今日收入
            TodayIncome: res.data.user.TodayIncome, //今日收入
            Spending: res.data.conclusion.Spending, //总支出
            Income: res.data.conclusion.Income, //总收入
            Refund: res.data.conclusion.Refund, //退款
            balance: res.data.user.price, //余额
          });

          if (that.data.page == 1) {
            lists = []
          }
          var list = res.data.wallet.data;
          if (list.length < that.data.pageSize) {
            that.setData({
              list: lists.concat(list),
              hasMoreData: false
            })
          } else {
            that.setData({
              list: lists.concat(list),
              page: that.data.page + 1,
              hasMoreData: true,
            })
          }
        } else {
          wx.showToast({
            title: '暂无数据',
            duration: 1000
          })
        }

      },
    })
  },
  // 开启选择器
  openActionsheet() {
    this.setData({
      'show': true
    });
  },
  // 关闭选择器
  closeActionSheet() {
    this.setData({
      'show': false
    });
  },
  // 根据选择进行调用事件
  clickAction({
    detail
  }) {
    var that = this
    that.setData({
      page: 1
    })
    const {
      index
    } = detail;
    this.setData({
      [`actions[${index}].loading`]: true,
      actionType: index
    });
    // 0提现到余额 1银行卡 2 支付宝 参数nickname：昵称  alipay：支付宝账号
    switch (index) {
      case 0: // 0 提现到银行卡
        wx.navigateTo({
          url: '/pages/driver/withdraw-money/index?type=1' + '&&yue=' + that.data.balance + "&&distribution=" + that.data.distribution,
        });
        break;
      case 1: // 1 提现到支付宝
        wx.navigateTo({
          url: '/pages/driver/withdraw-ali/index?type=2' + '&&yue=' + that.data.balance + "&&distribution=" + that.data.distribution,
        });
        break;
      case 2: // 1 提现到余额
        wx.navigateTo({
          url: '/pages/driver/withdraw-wechat/index?type=0' + '&&yue=' + that.data.balance + "&&distribution=" + that.data.distribution,
        });
        break;
    }
    setTimeout(() => {
      this.setData({
        [`show`]: false,
        [`actions[${index}].loading`]: false
      });
    }, 100);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    if (this.data.hasMoreData) {
      this.get_info_list()
    } else {
      wx.showToast({
        title: '没有更多数据',
        duration: 1000,
        icon: 'none'
      })
    }
  },
  // 用户分享
  onShareAppMessage: function (e) {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;
    console.log(app.shareAppMessage())
    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
})